package demo_09_26_05;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import class_01.demo_01;

/**
 * @author:连志鹏
 * @className:work_01
 * @descrption:
 * @date: 2021/9/25 20:51
 * @version: 0.1
 * @since: 1.8
 */
public class work_01 {

    static demo_01 demo = new demo_01();

    static ArrayList<user> list = new ArrayList<user>();

    static  user admin = new user("0","0",0);
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);



        Boolean bool = true;
        int ret = 0;String str = null;

        while (true)
        {
            menu();
            System.out.println("请选择菜单：");
            ret = sc.nextInt();
            switch (ret)
            {
                case 1:
                    register();
                    break;
                case 2:
                    land();
                    break;
                case 3:
                    luckDraw();
                    break;
                case 4:
                    System.out.println(list.toString());
                default:
                    System.out.println("输入有误!,重新输入：");
                    break;
            }
            System.out.println("是否继续？y or n");str = sc.next();
            if(!str.equals("y"))
                break;


        }




    }

    public static void land()
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入用户名：");
        String username = sc.next();
        System.out.print("请输入密码：");
        String password = sc.next();
        while (true)
        {

            int ret = judge(username,list);
            if(ret==-1){
                System.out.println("请重新输入用户名");
                username = sc.next();
                continue;
            }
            else if(list.get(ret).getPassword().equals(password))
            {
                admin = list.get(ret);
                System.out.println("登陆成功！欢迎你"+admin.getUsername());
                break;
            }else{
                System.out.println("请重新输入密码");
                password = sc.next();
                continue;
            }


        }

    }
    public static void luckDraw()
    {
        int[] num = new int[4];
        for (int i = 0; i < num.length; i++) {
            num[i] = demo.random_01(1000,10000);
            if(num[i] == admin.getNumber())
                System.out.println("恭喜你，幸运会员！");
        }
        System.out.println("幸运数字："+Arrays.toString(num));
        System.out.println("会员编号："+admin.getNumber());
    }

    public static void register()
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入用户名：");
        String username = sc.next();
        while (true)
        {
            if(judge(username,list)!=-1){
                System.out.println("请重新输入");
                username = sc.next();
            }
            else
                break;

        }
        System.out.print("请输入密码：");
        String password = sc.next();
        int number = demo.random_01(1000,10000);
        user u = new user(username,password,number);
        list.add(u);
        System.out.println("添加成功");
        System.out.println();

    }
    public static int judge(String str,ArrayList<user> users){
        for (int i = 0; i < users.size(); i++) {
            if(users.get(i).getUsername().equals(str))
                return i;
        }
        return -1;
    }


    public static void menu()
    {
        System.out.println("*****欢迎进入抽奖系统*****");
        System.out.println("         1.注册          ");
        System.out.println("         2.登陆          ");
        System.out.println("         3.抽奖          ");
        System.out.println("*************************");

    }

}

class user{
    private String username;
    private String password;
    private int number;

    public user(){}
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public user(String username, String password, int number) {

        this.username = username;
        this.password = password;
        this.number = number;
    }

}
